package com.tanhua.server.interceptor;

import com.tanhua.model.domain.User;

/**
 * 绑定线程,并赋值线程中的用户数据
 */
public class UserHolder {

    private static ThreadLocal<User> threadLocal = new ThreadLocal<>();

    /**
     * 存储数据到线程中
     * @param user
     */
    public static void set(User user){
        threadLocal.set(user);
    }

    /**
     * 获取线程中的数据
     */
    public static User get(){
        return threadLocal.get();
    }

    /**
     * 获取当前线程用户的手机号
     */
    public static String getMobile(){
        if (threadLocal.get() == null){
            return null;
        }
        return threadLocal.get().getMobile();
    }

    /**
     * 获取当前线程用户的id
     */
    public static Long getUserId(){
        if (threadLocal.get() == null){
            return null;
        }
        return threadLocal.get().getId();
    }

    /**
     * 删除线程中的数据
     */
    public static void remove(){
        threadLocal.remove();
    }
}
